home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / hAWK project / AWK Source / hAWK_prompt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-28  |  5.8 KB  |  269 lines  |  [TEXT/KEEN]

  1. /* hAWK_prompt.c : prompt user for a string */
  2. /* Copyright © 1986, 1988, 1989 1991 the Free Software Foundation, Inc.
  3.  *         This file is part of GAWK, the GNU implementation of the
  4.  * AWK Progamming Language, modified for the Macintosh (also called hAWK).
  5.  *         GAWK is free software; you can redistribute or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 1, or any later version.
  8.  *         GAWK is distributed in the hope that it will be useful,
  9.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11.  * GNU General Public License for more details.
  12.  *         You should have received a copy of the GNU General Public License
  13.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  14.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  16.  * Revision July 92, prompt and progress disabled if running concurrently
  17.  * with calling application.
  18.  */
  19. #include "AWK.H"
  20.  
  21. #define PROMPTID     408
  22. #define ASK            3
  23. #define ANSWER        4
  24.  
  25. static DialogPtr    progressPtr;
  26. #define PROGRESSID    409
  27.  
  28. static void get_one_string(NODE *tree, NODE **res);
  29. NODE *do_prompt(NODE *tree);
  30. char *Prompt(char *ask, short askLen);
  31. static pascal Boolean
  32. PromptFilter(DialogPtr theDlog, EventRecord *theEvent, short *itemHit);
  33. NODE *do_progress(NODE *tree);
  34. short Progress(char *msg, short msgLen);
  35. void UpdateProgress(void);
  36. void DisposeProgress(void);
  37.  
  38. static void get_one_string(NODE *tree, NODE **res)
  39.     {
  40.     if (!tree)
  41.         {
  42.         *res = WHOLELINE;
  43.         return;
  44.         }
  45.     *res = tree_eval(tree->lnode);
  46.     }
  47.  
  48. /* Display dialog with prompt asking for string. Return string. */
  49. NODE *do_prompt(NODE *tree)
  50.     {
  51.     NODE *t1, *t2;
  52.     char *answer;
  53.  
  54.     get_one_string(tree, &t1);
  55.     t1 = force_string(t1);
  56.     answer = Prompt(t1->stptr, t1->stlen);
  57.     t2 = tmp_string(answer, (short)strlen(answer));
  58.     free(answer);
  59.     free_temp(t1);
  60.     return t2;
  61.     }
  62.  
  63. /* Display dialog with prompt message (ask), return
  64. reply typed in edit field. For display purposes, all
  65. '\n' are converted to '\r'. */
  66. char *Prompt(char *ask, short askLen)
  67.     {
  68.     DialogPtr        dPtr;
  69.     char            bufStr[256],
  70.                     *answer,
  71.                     *tPtr,
  72.                     *endPtr,
  73.                     oldByte;
  74.     short                i, itemHit, maxByte = 0, nl[6];
  75.     extern Boolean gConcurrent;
  76. #ifdef powerc
  77.     ModalFilterUPP    filterUPP;
  78. #endif
  79.     
  80.     if (gConcurrent)
  81.         return(NULL);
  82.     
  83.     if (askLen < 0)
  84.         return(NULL);
  85.     
  86.     if (!GetAndAlignDialog(PROMPTID))
  87.         return(NULL);
  88.     dPtr = GetNewDialog(PROMPTID, NULL, (WindowPtr)-1L);
  89.     if (!dPtr)
  90.         return(NULL);
  91.     if (askLen > 255)
  92.         {
  93.         maxByte = 255;
  94.         oldByte = *(ask+maxByte);
  95.         *(ask+maxByte) = '\0';
  96.         }
  97.     tPtr = ask;
  98.     endPtr = tPtr + askLen;
  99.     for (i = 0; i < 6; ++i)
  100.         nl[i] = -1;
  101.     i = 0;
  102.     while (tPtr < endPtr)
  103.         {
  104.         if (*tPtr == '\n')
  105.             {
  106.             nl[i++] = tPtr - ask;
  107.             *tPtr = '\r';
  108.             if (i >= 6)
  109.                 break;
  110.             }
  111.         ++tPtr;
  112.         }
  113.     CtoPstr(ask);
  114.     SetEText(dPtr, ASK, (StringPtr)ask);
  115.     PtoCstr((StringPtr)ask);
  116.     if (nl[0] >= 0)
  117.         {
  118.         i = 0;
  119.         while (i < 6 && nl[i] >= 0)
  120.             *(ask+nl[i++]) = '\n';
  121.         }
  122.     if (maxByte)
  123.         *(ask+maxByte) = oldByte;
  124.     
  125.     ShowWindow(dPtr);
  126.     SetPort(dPtr);
  127.     FrameDialogItem(dPtr,1); /* admittedly lazy... */
  128. #ifdef powerc
  129.     filterUPP = NewModalFilterProc(PromptFilter);
  130.     ModalDialog(filterUPP, &itemHit);
  131.     DisposeRoutineDescriptor(filterUPP);
  132. #else
  133.     ModalDialog(PromptFilter, &itemHit);
  134. #endif
  135.     if (itemHit == 2) /* cancel */
  136.         {
  137.         DisposDialog(dPtr);
  138.         return(NULL);
  139.         }
  140.     GetEText(dPtr, ANSWER, (StringPtr)bufStr);
  141.     PtoCstr((StringPtr)bufStr);
  142.     itemHit = (short)strlen(bufStr);
  143.     emalloc(answer, char *, itemHit + 1, "Prompt");
  144.     memcpy(answer, bufStr, itemHit + 1);
  145.     DisposDialog(dPtr);
  146.     UpdateProgress();
  147.     return(answer);
  148.     }
  149.  
  150. /*  Filter for prompt dialog; treat <Command><Return> as
  151. a literal return. Treat <Return> or <Enter> as "OK". */
  152. static pascal Boolean
  153. PromptFilter(DialogPtr theDlog, EventRecord *theEvent, short *itemHit)
  154.     {
  155.     register short    key;
  156.  
  157.     /* Just catching key events */
  158.     if (theEvent->what != keyDown)
  159.         return (FALSE);
  160.  
  161.     key = (unsigned char) theEvent->message;
  162.  
  163.     if ((theEvent->modifiers & cmdKey)
  164.         && key == '\r') /* command - return */
  165.         {
  166.         TEKey ('\r', ((DialogPeek)theDlog)->textH);
  167.         theEvent->what = nullEvent;
  168.         return (FALSE);
  169.         }
  170.     else if (key == '\r' || key == '\003')
  171.         {
  172.         *itemHit = 1;
  173.         return (TRUE);
  174.         }
  175.     return (FALSE);
  176.     }
  177.  
  178. /* Display message on the go to report progress. Return number of
  179. times Progress has been called. */
  180. NODE *do_progress(NODE *tree)
  181.     {
  182.     NODE     *t1, *t2;
  183.  
  184.     get_one_string(tree, &t1);
  185.     t1 = force_string(t1);
  186.     t2 = tmp_number((AWKNUM)Progress(t1->stptr, t1->stlen));
  187.     free_temp(t1);
  188.     return t2;
  189.     }
  190.  
  191. short Progress(char *msg, short msgLen)
  192.     {
  193.     short                maxByte = 0;
  194.     char            *tPtr,
  195.                     *endPtr,
  196.                     oldByte;
  197.     Boolean            first = FALSE;
  198.     static short        numCalls;
  199.     short                i, nl[6];
  200.     extern Boolean gConcurrent;
  201.     
  202.     if (gConcurrent)
  203.         return(0);
  204.     
  205.     if (msgLen < 0)
  206.         return(0);
  207.     if (!progressPtr)
  208.         {
  209.         first = TRUE;
  210.         if (!GetAndAlignDialog(PROGRESSID))
  211.             return(0);
  212.         progressPtr = GetNewDialog(PROGRESSID, NULL, (WindowPtr)-1L);
  213.         if (!progressPtr)
  214.             return(0);
  215.         }
  216.     if (msgLen > 255)
  217.         {
  218.         maxByte = 255;
  219.         oldByte = *(msg+maxByte);
  220.         *(msg+maxByte) = '\0';
  221.         }
  222.     tPtr = msg;
  223.     endPtr = tPtr + msgLen;
  224.     for (i = 0; i < 6; ++i)
  225.         nl[i] = -1;
  226.     i = 0;
  227.     while (tPtr < endPtr)
  228.         {
  229.         if (*tPtr == '\n')
  230.             {
  231.             nl[i++] = tPtr - msg;
  232.             *tPtr = '\r';
  233.             if (i >= 6)
  234.                 break;
  235.             }
  236.         ++tPtr;
  237.         }
  238.     CtoPstr(msg);
  239.     SetEText(progressPtr, 1, (StringPtr)msg);
  240.     PtoCstr((StringPtr)msg);
  241.     if (nl[0] >= 0)
  242.         {
  243.         i = 0;
  244.         while (i < 6 && nl[i] >= 0)
  245.             *(msg+nl[i++]) = '\n';
  246.         }
  247.     if (maxByte)
  248.         *(msg+maxByte) = oldByte;
  249.     if (first)
  250.         {
  251.         ShowWindow(progressPtr);
  252.         DrawDialog(progressPtr);
  253.         }
  254.     
  255.     return(++numCalls);
  256.     }
  257.  
  258. void UpdateProgress()
  259.     {
  260.     if (progressPtr)
  261.         DrawDialog(progressPtr);
  262.     }
  263.  
  264. void DisposeProgress()
  265.     {
  266.     if (progressPtr)
  267.         DisposDialog(progressPtr);
  268.     }
  269.